package com.im.app.manager;

import com.im.app.model.Conversations;
import org.jim.core.ImPacket;
import org.jim.core.config.ImConfig;
import org.jim.core.packets.Command;
import org.jim.core.packets.Group;
import org.jim.core.packets.RespBody;
import org.jim.server.JimServerAPI;
import org.jim.server.config.ImServerConfig;
import org.jim.server.util.ChatKit;

import java.util.HashMap;
import java.util.Map;

public class GroupManager {

    private static GroupManager manager;

    public GroupManager(){

    }

    public Map<String, Group> groupMap = new HashMap<>();

    public static GroupManager getManager(){
        if(manager == null){
            synchronized (GroupManager.class){
                if(manager == null){
                    manager = new GroupManager();
                }
            }
        }
        return manager;
    }

    public void sendGroupChange(String userId,Group group){
        ImServerConfig imServerConfig = ImConfig.Global.get();
        boolean isStore = ImServerConfig.ON.equals(imServerConfig.getIsStore());
        if(ChatKit.isOnline(userId,isStore)){
            ImPacket imPacket = new ImPacket(Command.COMMAND_CHANGE_GROUP_RESP,new RespBody(Command.COMMAND_CHANGE_GROUP_RESP,group).toByte());
            JimServerAPI.sendToUser(userId,imPacket);
        }
    }


    public void cacheGroup(String key,Group group){
        groupMap.put(key,group);
    }

    public Group getGroup(String key){
        Group group = groupMap.get(key);
        return group;
    }

    public void removeGroup(String key){
        groupMap.remove(key);
    }




}
